package Utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DateBaseInquiryUtils {

	/*
	 * 注册用户
	 */
	public static int reg(String name, String password) {
		ResultSet rs = null;
		Statement stmt = null;
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");

			conn = DriverManager.getConnection(
					"jdbc:mysql://123.206.212.238/Train?useUnicode=true&characterEncoding=utf8", "root", "1111");
			stmt = conn.createStatement();

			rs = stmt.executeQuery("SELECT password FROM user WHERE name = '" + name + "'");
			while (rs.next()) {
				return 0;
			}

			String sql = null;
			sql = "INSERT INTO user (NAME,PASSWORD) VALUE ('" + name + "','" + password + "')";
			int n = stmt.executeUpdate(sql);

			return n;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	/*
	 * 返回密码
	 */
	public static String getPass(String name) {

		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://123.206.212.238/Train?useUnicode=true&characterEncoding=utf8", "root", "1111");
			Statement stmt = conn.createStatement();

			ResultSet rs = stmt.executeQuery("SELECT password FROM user WHERE name = '" + name + "'");
			while (rs.next()) {
				// if (rs.getString(2).equals("")) {

				return rs.getString(1);
			}

			rs.close();
			stmt.close();
			conn.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		return "";
	}

	/*
	 * 根据用户名，返回好友列表
	 */
	public static String FriendList(String user) {
		try {
			Class.forName("com.mysql.jdbc.Driver");

			Connection conn;
			conn = DriverManager.getConnection(
					"jdbc:mysql://123.206.212.238/Train?useUnicode=true&characterEncoding=utf8", "root", "1111");
			Statement stmt = conn.createStatement();

			ResultSet rs = stmt.executeQuery("SELECT Friends FROM user WHERE name = '" + user + "'");
			while (rs.next()) {
				return rs.getString(1);
			}

			rs.close();
			stmt.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return null;
	}

	/*
	 * 返回好友列表
	 */
	public static String[] inquiry(String mySelf) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn;
			conn = DriverManager.getConnection(
					"jdbc:mysql://123.206.212.238/Train?useUnicode=true&characterEncoding=utf8", "root", "1111");
			Statement stmt = conn.createStatement();

			ResultSet rs = stmt.executeQuery("SELECT Friends FROM user WHERE name = '" + mySelf + "'");
			while (rs.next()) {
				String msg = rs.getString(1);
				if (msg != null) {
					String[] friends = rs.getString(1).split(",");
					return friends;
				} else
					return null;
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return null;
	}
	
}
